<?php
namespace Home\Controller;
use Think\Controller;
class GoodsController extends Controller {
    public function showAdd(){

        $this->display();
    }
    public function getAllCate(){
        $cate=M('category');
        $data=$cate->select();
        $this->ajaxReturn($data);
    }

    public function addGoods(){
        $goods=M('goods');
        $data=$goods->create();
        //$success=$goods->add($data);
        //$goods_img=$_POST['goods_img'];
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
        $upload->savePath =  '';

        $info   =   $upload->uploadOne($_FILES['goods_img']);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
            $data['goods_img']='./uploads/'.$info['savepath'].$info['savename'];

            $id=$goods->add($data);
            if($id){
                echo "商品添加成功";
                echo "<a href='/JD/index.php/Home/Goods/showAdd'> 返回添加页</a>"."<br/>";
                echo "<a href='/JD/index.php/Home/ShowList'> 返回商品页</a>";
            }else{
                echo "商品添加失败";
            }

        }
        //var_dump($data);


    }

}